home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / apb17.zip / TIMER.ASM < prev    next >
Assembly Source File  |  1990-12-20  |  3KB  |  127 lines

  1.     Title Timer routines for ApBasic
  2.     Page 60,130
  3. ;    Created 10-5-1987 k. murray
  4. ;
  5. Cseg    Segment byte public 'Code'
  6.     Assume Cs:Cseg,Ds:nothing,Es:nothing
  7. Start:
  8.     Jmp    Timer_On    ; Turn timer on
  9.     Jmp    Timer_Off    ; Turn timer off and reset INT
  10.     Jmp    Timer_Reset    ; Reset timer counter to zero
  11. ;
  12. ; Some data locations
  13. Old_Int1c Dw    0,0        ; Place to hold old Int 1ch
  14. TrapMask  Dw    0,0        ; mask to set trap word for our user number
  15. UserTicks Dw    0        ; User supplied number of ticks
  16. TotalTicks Dw    0        ; Counter Dec by interrupt routine
  17. ProgDs    Dw    0        ; Program's Data segment for int routine
  18. ;
  19. ;    SUB Timer.Reset
  20. ;
  21. Timer_Reset Proc Far
  22.     Mov    Ax,Cs:UserTicks
  23.     Mov    Cs:TotalTicks,Ax    ; Reset counter
  24.     Ret
  25. Timer_Reset Endp
  26. ;
  27. ;    SUB Timer.Off
  28. ;
  29. Timer_Off Proc Far
  30.     Mov    Ax,251ch
  31.     Push    Ds
  32.     Lds    Dx,dword ptr Old_Int1c    ; get adr. of vector in Ds:Dx
  33.     Int    21h            ; set old int back
  34.     Pop    Ds
  35.     Ret
  36. Timer_Off Endp
  37. ;
  38. ;    SUB Timer.On Ticks%,Userno%
  39. ;    Stack Frame:
  40. ;    +8    Ticks% adr.
  41. ;    +6    Userno% adr.
  42. ;    +4    Return Segment
  43. ;    +2    Return offset
  44. ;    +0    Saved Bp
  45. ;
  46. Timer_On Proc Far
  47.     Push    Bp
  48.     Mov    Bp,Sp
  49.         ; generate a trap mask from the Userno% variable
  50.     Mov    Bx,[Bp+6]        ; Get user number adr.
  51.     Mov    Cx,[Bx]            ; Get user number
  52.     Dec    Cx            ; make relative to zero
  53.     Cmp    Cx,32
  54.     Jb    Timer_On2
  55.     Mov    Cx,32            ; use smallest
  56. Timer_On2:
  57.     Xor    Ax,Ax
  58.     Mov    Dx,Ax
  59.     Inc    Ax            ; set low bit
  60.     Clc                ; clear carry
  61.     Jcxz    Timer_On6
  62. Timer_On4:
  63.     Rcl    Ax,1
  64.     Rcl    Dx,1            ; shift dword over one bit
  65.     Loop    Timer_On4
  66. Timer_On6:
  67.     Mov    Cs:TrapMask,Ax
  68.     Mov    Cs:TrapMask+2,Dx    ; save mask
  69.         ; Save the Ticks% variable
  70.     Mov    Bx,[Bp+8]        ; Get Ticks% adr.
  71.     Mov    Ax,[Bx]            ; get Ticks%
  72.     Mov    Cs:UserTicks,Ax        ; save user ticks
  73.         ; Setup for doing the Interrupt
  74.     Push    Es
  75.     Push    Ds
  76.     Mov    Ax,351ch
  77.     Int    21h            ; Get old Int1c vector
  78.     Mov    Cs:Old_Int1c,Bx
  79.     Mov    Cs:Old_Int1c+2,Es    ; save it
  80.     Push    Cs
  81.     Pop    Ds
  82.     Mov    Dx,Offset Timer_Int    ; Adr. of timer routine in Ds:Dx
  83.     Mov    Ax,251ch
  84.     Int    21h            ; Set new Int1c vector
  85.     Pop    Ds
  86.     Pop    Es
  87.         ; set initial timer value
  88.     Mov    Ax,Cs:UserTicks
  89.     Mov    Cs:TotalTicks,Ax    ; Reset counter
  90.     Mov    Cs:ProgDs,Ds        ; save current data segment
  91.         ; Clean up the parameters and return
  92.     Pop    Bp
  93.     Ret    4    ; Two parameters at 2 bytes/param
  94. Timer_On Endp
  95. ;
  96. ;
  97. ;    Timer interrupt routine. This routine is called by Int 1ch.
  98. ;    When the TotalTicks variable is zero, then this routine set's
  99. ;    the user bit defined in Timer_On.
  100. Timer_Int:
  101.     Push    Ax
  102.     Push    Dx
  103.     Push    Ds            ; save some registers
  104. ;
  105.     Push    Cs
  106.     Pop    Ds            ; Ds=Cs
  107.     Dec    word ptr TotalTicks    ; Dec counter
  108.     Jnz    Timer_Int2        ; not zero yet
  109.  
  110.         ; User TrapMask to set trap bit in trap dword in base page
  111.     Mov    Ax,UserTicks
  112.     Mov    TotalTicks,Ax        ; Restore counter
  113.     Mov    Ax,TrapMask
  114.     Mov    Dx,TrapMask+2
  115.     Push    word ptr ProgDs
  116.     Pop    Ds
  117.     Or    word ptr Ds:[0],Ax
  118.     Or    word ptr Ds:[2],Dx    ; set timer bit in trap dword
  119. Timer_Int2:
  120. ;
  121.     Pop    Ds
  122.     Pop    Dx
  123.     Pop    Ax        ; Restore the registers
  124.     Iret
  125. Cseg    Ends
  126.     End    Start
  127.